package cn.xorange.commons.security.filter.handler;

import cn.xorange.commons.security.constants.HeaderConstants;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.exception.SecurityErrorCode;
import cn.xorange.commons.security.filter.FilterHandler;
import cn.xorange.commons.security.filter.LoginFilterService;
import cn.xorange.commons.security.service.LoginHandler;
import cn.xorange.commons.security.service.LoginHandlerFactory;
import cn.xorange.commons.utils.cache.ICacheService;
import cn.xorange.commons.utils.captcha.Captcha;
import cn.xorange.commons.utils.captcha.CaptchaService;
import cn.xorange.commons.utils.configuration.constants.UtilsErrorCode;
import cn.xorange.commons.utils.http.ServletUtils;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author : yangjian
 * @date : 2023/12/23
 */
@Slf4j
public class CaptchaFilterHandler implements FilterHandler {

    @Resource
    CaptchaService captchaService;
    @Resource
    LoginHandlerFactory loginHandlerFactory;
    @Resource
    LoginFilterService loginFilterService;
    @Resource
    ICacheService cacheService;


    @Override
    public String url() {
        return SecurityConstants.URL_CAPTCHA;
    }

    @Override
    public Boolean passToken() {
        return true;
    }

    @Override
    public void handler(HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = ServletUtils.getParamJsonObj(request);
        boolean isMath = jsonObject.getBooleanValue("isMath");
        boolean isImg = jsonObject.getBooleanValue("isImg");
        Captcha captcha = this.captchaService.getVerifyCode(isMath,isImg);

        String key = captcha.cacheKey();
        String sysType = request.getHeader(HeaderConstants.X_SYS_TYPE);
        String loginType = jsonObject.getString(SecurityConstants.LOGIN_TYPE);
        if(Objects.isNull(sysType)||Objects.isNull(loginType)){
            ServletUtils.responseOut(response, UtilsErrorCode.CAPTCHA_PARAM_ERROR.getStatus(),UtilsErrorCode.CAPTCHA_PARAM_ERROR.getCode(),UtilsErrorCode.CAPTCHA_PARAM_ERROR.getMsg(),null);
        }else {
            LoginHandler loginHandler = loginFilterService.getLoginHandler(sysType, loginType ,loginHandlerFactory);
            if(Objects.isNull(loginHandler)){
                SecurityErrorCode errorCode = SecurityErrorCode.LOGIN_HANDLER_NOT_FOUND;
                log.debug(errorCode.getMsg());
                ServletUtils.responseOut(response,errorCode.getStatus(),errorCode.getCode(),errorCode.getMsg(),null);
            }else {
                cacheService.setCacheObject(key,captcha.getCode(), loginHandler.config().getCaptchaExpire(), TimeUnit.SECONDS);
                log.debug("uuid:{} code:{} 有效时间:{} 分钟",captcha.getUuid(),captcha.getCode(), loginHandler.config().getCaptchaExpire()/60);
                ServletUtils.responseSuccessOut(response, captcha);
            }
        }

    }
}
